﻿@model IPagedList<MVCApp.Models.Person>

@{
    int districtId = ViewBag.DistrictId;
    string districtName = ViewBag.DistrictName;

    ViewBag.Title = districtName + " - " + Strings.SearchAdresses;
}

<h2>@Html.ActionLink(districtName, "Index", "District", new { id = districtId }, null) -  @Strings.SearchAdresses</h2>

@if (Model.IsFirstPage)
{
<div>
    <div class="button buttonIn">
        <a href="#" id="search" class="withImage">
            <img src='~/Images/WindowsIcons/Light/appbar.refresh.svg'/> @Strings.SearchRunOnKrak
        </a>
    </div> 

    <div class="button">
        <a href="@Url.Action("PreviousSearch", "SearchAddress", new { id = districtId })">
            @Strings.SearchAddressesPrevious
        </a>
    </div>
    <div class="spacer" />
</div>


    <div id="ajaxLoader" style="display: none">
        <img src="~/Images/ajax-loader.gif"> <span id="progressMessage"></span>
    </div>
    <div id="searchError" class="warningMessage" style="display: none"></div>
}

<div id="personList">
    @Html.Partial("_PersonList")
</div>

@if (Model.Count == 0)
{
<script>
    $('#search').click();
</script>
}

@section scripts {
    <!--Script references. -->
    <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
    <!--Reference the SignalR library. -->
    <script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.--> 
    <script>
        $(function () {
            
            var modelCount = @Model.Count;

            // Reference the auto-generated proxy for the hub.  
            var searchAddressHub = $.connection.searchAddressHub;
            
            searchAddressHub.client.setProgressMessage = function (message) {
                $('#progressMessage').text(message);
            };

            searchAddressHub.client.searchError = function (message) {
                $('#ajaxLoader').hide();
                $('#searchError').text(message);
                $('#searchError').show();
                $('#search').removeClass("pressed");
            };

            searchAddressHub.client.searchComplete = function (addressFound) {

                $.ajax({
                    url : '@Url.Action("GetAddressList")',
                    data:{"id":@districtId, "addressFound":addressFound},
                    type: 'GET',

                    success: function(data){
                        $('#personList').html(data);
                        $('#ajaxLoader').hide();
                        $('#search').removeClass("pressed");
                    }
                });                
            };                      

            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#search').click(function (e) {

                    e.preventDefault();

                    var modelCount = @Model.Count;

                    var confirmed = true;
                    if(modelCount != 0) {
                        confirmed = confirm('@Html.Raw(Strings.SearchAdressConfirm)');
                    }

                    if (confirmed == true) {
                        searchAddressHub.server.search(@districtId);
                        $('#ajaxLoader').show();
                        $('#searchError').hide();
                        $('#search').addClass("pressed");
                    } 
                });

                if(modelCount == 0) {
                    $('#search').click();
                }
            });            
        });
    </script>
}